# -*- coding: utf-8 -*-
from airflow.providers.dingding.operators.dingding import DingdingHook
from airflow.providers.presto.hooks.presto import PrestoHook


def check_dingding(env, db, table, where, dt, desc='', presto_conn_id='presto_default', dingding_conn_id='dingding_default',
                   at_mobiles=None, at_all=False):
    env = env
    ph = PrestoHook(presto_conn_id=presto_conn_id)
    sql = f"SELECT count(1) AS num FROM {db}.{table} where {where}"
    print('sql', sql)
    cnt = ph.get_records(sql)
    title = f"【{env}】数据量监控"
    dt = dt
    text = f"""
# {title}
   - *表名* ：{table}
   - *分区* ：{dt}
   - *数量* ：{cnt}
   - *说明* ：{desc}
"""
    return DingdingHook(dingding_conn_id=dingding_conn_id,
                        message_type='markdown',  # text, link, markdown, actionCard, feedCard
                        message={'title': title, 'text': text},
                        at_mobiles=at_mobiles,
                        at_all=at_all, ).send()

#
# def check_dingding(db, table, where, dt, presto_conn_id='presto_default', dingding_conn_id='dingding_default',
#                    at_mobiles=None, at_all=False):
#     def _check_dingding(ctx):
#         """
#         :param ctx: dict 包含 ds, dag_run 等所有 jinja 模板内可使用的变量
#         :return:
#         """
#         env = ctx['var']['value'].env
#         ph = PrestoHook(presto_conn_id=presto_conn_id)
#         sql = f"SELECT count(1) AS num FROM {db}.{table} where {where}"
#         print('sql', sql)
#         cnt = ph.get_records(sql)
#         title = f"{env.upper()}数据量监控"
#         # dt = dt
#         text = f"""
# # {title}
#    - *表名* ：{table}
#    - *分区* ：{dt}
#    - *数量* ：{cnt}
# """
#         return DingdingHook(dingding_conn_id=dingding_conn_id,
#                             message_type='markdown',  # text, link, markdown, actionCard, feedCard
#                             message={'title': title, 'text': text},
#                             at_mobiles=at_mobiles,
#                             at_all=at_all, ).send()
#
#
#
#     return _check_dingding
